package com.company.model;

import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.codec.binary.Base64;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Goods {
    Integer id;
    String name;
    double price;
    int stock;
    String type;
    String introduction;
    String src;

    String base64code;
    public void loadImage() throws IOException {
        FileInputStream inputStream = new FileInputStream("uploads/" + src);
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        byte [] b = new byte[1024];
        int len = inputStream.read(b);
        while(len != -1) {
            byteArrayOutputStream.write(b, 0, len);
            len = inputStream.read(b);
        }

        byte [] filebyte = byteArrayOutputStream.toByteArray();
        base64code = "data:image;base64," + new String(Base64.encodeBase64(filebyte));
    }
}
